
package com.ruoyi.web.controller;

import com.ruoyi.common.core.core.domain.AjaxResult;
import com.ruoyi.common.security.utils.LoginHelper;
import com.ruoyi.common.web.core.BaseController;
import com.ruoyi.system.domain.Form;
import com.ruoyi.system.service.IFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaIgnore;

import java.util.Map;

@RestController
@RequestMapping("/form")
public class FormController extends BaseController {

    @Autowired
    private IFormService formService;

    @GetMapping("/list")
    public AjaxResult list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Form form) {
        form.setTenantId(LoginHelper.getTenantId());
        return success(formService.selectFormList(form, pageNum, pageSize));
    }

    @SaIgnore
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(formService.selectFormById(id));
    }

    @PostMapping
    public AjaxResult add(@RequestBody Form form) {
        form.setTenantId(LoginHelper.getTenantId());
        return success(Map.of("id", formService.insertForm(form)));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody Form form) {
        form.setTenantId(LoginHelper.getTenantId());
        return success(formService.updateForm(form));
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        return success(formService.deleteFormById(id));
    }

    @SaIgnore
    @GetMapping("/byName/{formName}")
    public AjaxResult getFormByName(
            @PathVariable String formName,
            @RequestParam(required = true) Long tenantId) {
        return success(formService.selectFormByName(formName, tenantId));
    }
}
